#include "testwidget.h"



#include <vtkAxis.h>
#include <vtkBrush.h>
#include <vtkChartHistogram2D.h>
#include <vtkColor.h>
#include <vtkColorLegend.h>
#include <vtkColorTransferFunction.h>
#include <vtkContextScene.h>
#include <vtkContextView.h>
#include <vtkDoubleArray.h>
#include <vtkImageData.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTable.h>
#include <vtkTextProperty.h>
#include <vtkVector.h>
#include <vtkContextActor.h>
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
//#include <vtkQtView.h>
#include <vtkPolyDataMapper.h>
#include "vtkPolygon.h"
#include "vtkProperty.h"
#include "vtkUnsignedCharArray.h"
#include "vtkMinimalStandardRandomSequence.h"
#include "vtkCellData.h"
#include "vtkTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkAlgorithmOutput.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkTubeFilter.h"
#include "vtkSphereSource.h"
#include "vtkGlyph3D.h"
#include "vtkGlyph3DMapper.h"
#include "vtkPointData.h"
#include "vtkOrientationMarkerWidget.h"
#include "vtkAxesActor.h"
#include "vtkCaptionActor2D.h"
#include "vtkPropAssembly.h"
#include "vtkAnnotatedCubeActor.h"
#include "vtkInteractorObserver.h"

#include <QVTKOpenGLNativeWidget.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <array>

#include "QGridLayout"


TestWidget::TestWidget(QWidget *parent) : QWidget(parent) {

//    this->layout = new QGridLayout(this);
//    this->layout->setMargin(0);


    vtkNew<vtkNamedColors> colors;

    vtkNew<vtkGenericOpenGLRenderWindow> window;
    window->SetMultiSamples(0);
    vtkNew<vtkRenderer> renderer;
    window->AddRenderer(renderer);


    renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());


    vtkCamera* camera = renderer->GetActiveCamera();
//          renderer->ResetCamera();
    camera->SetPosition(0, 10, 10);
    camera->Azimuth(30);
    camera->Elevation(30);

    this->window = window;
    this->renderer = renderer;
    this->camera = camera;

//
//    vtkNew<vtkNamedColors> colors;
//
//    vtkNew<vtkGenericOpenGLRenderWindow> window;
//    window->SetMultiSamples(0);
//    vtkNew<vtkRenderer> renderer;
//    window->AddRenderer(renderer);
//
//    renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
//
//
//    vtkCamera* camera = renderer->GetActiveCamera();
////          renderer->ResetCamera();
//    camera->SetPosition(0, 10, 10);
//    camera->Azimuth(30);
//    camera->Elevation(30);
//
////    vtkSmartPointer<vtkCamera> camera = renderer->GetActiveCamera();
////    renderer->ResetCamera();
////    camera->SetFocalPoint(0, 0, 0);
////    camera->SetViewUp(0, 1, 0);
////    camera->SetPosition(0, 0, 3500);
//////    camera->SetPosition(0, 0, 35000);
////    renderer->ResetCameraClippingRange();
//
////    vtkNew<vtkCubeSource> cube;
////    vtkNew<vtkPolyDataMapper> cubeMapper;
////    cubeMapper->SetInputConnection(cube->GetOutputPort());
////    vtkNew<vtkActor> actorCube;
////    cube->SetBounds(-1, 1, -1, 1, -1, 1);
////    actorCube->SetMapper(cubeMapper);
////    renderer->AddActor(actorCube);
//
////    vtkNew<vtkActor> actor;
////    // 关闭对光的反射和漫反射
////    actor->GetProperty()->SetAmbient(1.0);
////    actor->GetProperty()->SetDiffuse(0);
////    actor->GetProperty()->SetSpecular(0);
////    actor->GetProperty()->ShadingOff();
////    renderer->AddActor(actor);
//

    this->widget = new QVTKOpenGLNativeWidget(this);
    this->widget->setFixedSize(600, 600);
//    this->layout->addWidget(widget);
    this->widget->setRenderWindow(window);




    // 测试

    std::array<std::string, 3> xyzLabels{{"X", "Y", "Z"}};
    std::array<double, 3> scale{{1.0, 1.0, 1.0}};

    vtkNew<vtkAxesActor> axes111;
    axes111->SetScale(scale[0], scale[1], scale[2]);
    axes111->SetShaftTypeToCylinder();
    axes111->SetXAxisLabelText(xyzLabels[0].c_str());
    axes111->SetYAxisLabelText(xyzLabels[1].c_str());
    axes111->SetZAxisLabelText(xyzLabels[2].c_str());
    axes111->SetCylinderRadius(0.5 * axes111->GetCylinderRadius());
    axes111->SetConeRadius(1.025 * axes111->GetConeRadius());
    axes111->SetSphereRadius(1.5 * axes111->GetSphereRadius());
    vtkTextProperty* tprop = axes111->GetXAxisCaptionActor2D()->GetCaptionTextProperty();
    tprop->ItalicOn();
    tprop->ShadowOn();
    tprop->SetFontFamilyToTimes();
    // Use the same text properties on the other two axes.
    axes111->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy(tprop);
    axes111->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy(tprop);

    renderer->AddActor(axes111);

    this->axes111 = axes111;


    vtkNew<vtkOrientationMarkerWidget> om2;
    om2->SetOrientationMarker(this->axes111);
//    // Position lower right in the viewport.
    om2->SetViewport(0.8, 0, 1.0, 0.2);
////    om2->SetViewport(0.5, 0.5, 1.0, 0.2);
////    om2->SetViewport(0.0, 0.0, 0.2, 0.2);
    om2->SetDefaultRenderer(renderer);
    om2->SetInteractor(window->GetInteractor());
    om2->EnabledOn();
    om2->SetEnabled(1);
    om2->InteractiveOn();
    om2->Modified();

    // 经过测试 om2 必须保存起来
    this->om2 = om2;


//    window->Render();
////    window->GetInteractor()->Render();
}

